<template>
  <div id="app">
    <a-input :value="inputValue" placeholder="请输入任务" class="my_ipt" @change="inputChange"/>
    <a-button type="primary" @click="addItem">添加事项</a-button>

    <a-list bordered :dataSource="infoList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked="item.done" @change="checkedChange(item.id)">{{item.info}}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="removeItem(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{undone}}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button :type="key === 'all' ? 'primary' : 'default'" @click="changeList('all')">全部</a-button>
          <a-button :type="key === 'undone' ? 'primary' : 'default'" @click="changeList('undone')">未完成</a-button>
          <a-button :type="key === 'done' ? 'primary' : 'default'" @click="changeList('done')">已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="clear">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions, mapGetters } from 'vuex'
export default {
  name: 'app',
  data () {
    return {
    }
  },
  methods: {
    ...mapMutations(['setInputValue', 'addList', 'removeList', 'setChecked', 'clear', 'changeKey']),
    ...mapActions(['getList']),
    inputChange(e) {
      this.setInputValue(e.target.value)
    },
    checkedChange(id) {
      this.setChecked(id)
    },
    addItem() {
      if (this.inputValue.trim() === '') {
        return this.$message.warning('文本框内容不能为空')
      }
      this.addList()
    },
    removeItem(id) {
      this.removeList(id)
    },
    changeList(key) {
      this.changeKey(key)
    }
  },
  computed: {
    ...mapState(['inputValue', 'key']),
    ...mapGetters(['undone', 'infoList'])
  },
  created() {
    this.getList()
  }
}
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
